package gameResourceUtil

import (
	"fmt"
	"math"
	"runtime/debug"
	"strconv"

	"7qule.com/sd3/CrossServer/src/model/commonModel"

	_ "7qule.com/sd3/CrossServer/src/model/commonModel"
	"public.com/goutil/logUtil"
	"public.com/goutil/stringUtil"
)

// 处理重复的资源
func HandleDuplicateResource(gameResourceStr string) string {
	if gameResourceStr == "" {
		return ""
	}
	gameResourceList := ConvertToGameResourceObject(gameResourceStr)

	return ConvertToString(gameResourceList)
}

// 转换成游戏资源
func ConvertToGameResourceObject(gameResourceStr string) []*commonModel.GameResourceObject {
	if gameResourceStr == "" {
		return make([]*commonModel.GameResourceObject, 0, 0)
	}
	gameResourceStrList := stringUtil.Split(gameResourceStr, []string{"||"})

	newGameResourceList := make([]*commonModel.GameResourceObject, 0, len(gameResourceStrList))
	for _, v1 := range gameResourceStrList {
		temp1, err1 := stringUtil.SplitToInt32Slice(v1, ",")
		if err1 != nil {
			errMsg := fmt.Sprintf("gameResourceStr: %s , v1:%s,  errmsg:%s  stack:%s", gameResourceStr, v1, err1, debug.Stack())
			logUtil.Log(errMsg, logUtil.Error, true)

		}
		if len(temp1) != 3 {
			errMsg := fmt.Sprintf("gameResourceStr: %s , v1:%s,游戏资源错误", gameResourceStr, v1)
			logUtil.Log(errMsg, logUtil.Error, true)
		}

		isExist := false
		for _, v2 := range newGameResourceList {
			if v2.ResourceTypeSub == temp1[0] && v2.ModelId == temp1[1] {
				v2.Count += temp1[2]
				isExist = true
				break
			}
		}
		// 不存在则加入
		if isExist == false {
			newGameResourceList = append(newGameResourceList, commonModel.NewGameResourceObject(temp1[0], temp1[1], temp1[2]))
		}
	}
	return newGameResourceList
}

// 合并重复并翻倍
func MultipleGameResource(gameResourceStr string, multipleNum float64) string {
	if gameResourceStr == "" || multipleNum < 0 {
		return ""
	}

	gameResourceList := ConvertToGameResourceObject(gameResourceStr)

	for _, v := range gameResourceList {
		v.Count = int32(float64(v.Count) * multipleNum)
	}

	return ConvertToString(gameResourceList)
}

// 合并重复并计算加成(加成有可能是负数)
// 计算结果向下取整
func GameResourceStrAdd(gameResourceStr string, add float64) string {
	if gameResourceStr == "" {
		return ""
	}
	gameResourceList := ConvertToGameResourceObject(gameResourceStr)

	for _, v := range gameResourceList {
		v.Count = int32(math.Floor(float64(v.Count) * (1 + add)))
	}

	return ConvertToString(gameResourceList)
}

// 转换成string
func ConvertToString(gameResourceList []*commonModel.GameResourceObject) string {
	result := ""
	if gameResourceList == nil {
		return result
	}

	for _, v := range gameResourceList {
		if result != "" {
			result += "||"
		}
		result += strconv.Itoa(int(v.ResourceTypeSub)) + "," + strconv.Itoa(int(v.ModelId)) + "," + strconv.Itoa(int(v.Count))
	}
	return result
}

// 连接两个游戏资源字符串,并处理重复资源
func JoinGameResourceString(resource1, resource2 string) string {
	result := resource1
	if result == "" {
		result = resource2
	} else if resource2 != "" {
		result += "||" + resource2
	}
	gameResourceList := ConvertToGameResourceObject(result)

	return ConvertToString(gameResourceList)
}
